//
//  XDTabBarViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/13/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "XDTabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "FifthViewController.h"
#import "LoginViewController.h"
#import "XDHeader.h"
#import "XDTools.h"
#import "UserInfo.h"

#define btn_tag 10001

@interface XDTabBarViewController ()

@end

@implementation XDTabBarViewController
    DEFINE_SINGLETON_FOR_CLASS(XDTabBarViewController)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    FirstViewController * vc1 = [[FirstViewController alloc] init];
    SecondViewController * vc2 = [[SecondViewController alloc] init];
    ThirdViewController * vc3 = [[ThirdViewController alloc] init];
    FourthViewController * vc4 = [[FourthViewController alloc] init];
    FifthViewController * vc5 = [[FifthViewController alloc] init];
    
    switch (TABBARCOUNT){
        case 3:
            self.viewControllers = @[vc1,vc2,vc3];
            
            break;
        case 4:
            self.viewControllers = @[vc1,vc2,vc3,vc4];
            break;
        case 5:
            self.viewControllers = @[vc1,vc2,vc3,vc4,vc5];
            break;
        default:
            break;
    }
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH, UI_TAB_BAR_HEIGHT)];
    _bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tab_Bg"]];
    [self.tabBar addSubview:_bgView];

    for (int i = 0; i < TABBARCOUNT; i ++)
	{
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
		button.frame = CGRectMake(i*UI_SCREEN_WIDTH/TABBARCOUNT,0,UI_SCREEN_WIDTH/TABBARCOUNT,UI_TAB_BAR_HEIGHT);
		button.enabled = i==DEFAULTSELECTINDEX?NO:YES;
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_notOn",i+1]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_on",i+1]] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_on",i+1]] forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(chooseTabBar:) forControlEvents:UIControlEventTouchDown];
		button.adjustsImageWhenHighlighted =NO;
        button.tag = btn_tag+i;
        if (!button.enabled){
            [self setSelectedIndex:i];
            _beforeIndex = i;
            _presentIndex = i;
        }
		[_bgView addSubview:button];
	}
}

-(void)chooseTabBar:(UIButton *)sender
{
    _presentIndex = sender.tag-btn_tag;
    NSArray * array = NEEDACCOUNTFORLOGIN;
    if ([[array objectAtIndex:_presentIndex] isEqualToString:@"1"]){
        if (![UserInfo sharedUserInfo].isLogin){
            LoginViewController * loginView = [[LoginViewController alloc] init];
            [self presentViewController:loginView animated:YES completion:^{
                
            }];
            [self confirmSelectTabBar:_beforeIndex];
        }else{
            [self confirmSelectTabBar:_presentIndex];
            [self setSelectedIndex:_presentIndex];
            _beforeIndex = _presentIndex;
        }
    }else{
        [self confirmSelectTabBar:_presentIndex];
        [self setSelectedIndex:_presentIndex];
        _beforeIndex = _presentIndex;
    }
}

-(void)confirmSelectTabBar:(NSInteger)selectIndex
{
    for (UIButton * button in [_bgView subviews]){
        if ([button isKindOfClass:[UIButton class]]){
            button.enabled= button.tag==selectIndex+10001?NO:YES;
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end






//========================================================================================================

//-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//    _presentIndex = tabBarController.selectedIndex;
//    NSArray * array = NEEDACCOUNTFORLOGIN;
//    if([[array objectAtIndex:_presentIndex] isEqualToString:@"1"]){
//        LoginViewController * lvc = [[LoginViewController alloc] init];
//        if (![UserInfo sharedUserInfo].isLogin){
//            [tabBarController setSelectedIndex:_beforeIndex];
//            [self presentViewController:lvc animated:YES completion:^{
//                
//            }];
//        }else{
//            [tabBarController setSelectedIndex:_presentIndex];
//            _beforeIndex = _presentIndex;
//        }
//    }else{
//        [tabBarController setSelectedIndex:_presentIndex];
//        _beforeIndex = _presentIndex;
//    }
//    [self confirmSelectTabBar:_beforeIndex];
//}

//-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
//{
//    _presentIndex = tabBarController.selectedIndex;
//    NSArray * array = NEEDACCOUNTFORLOGIN;
//    if([[array objectAtIndex:_presentIndex] isEqualToString:@"1"]){
//        LoginViewController * lvc = [[LoginViewController alloc] init];
//        if (![UserInfo sharedUserInfo].isLogin){
//            [tabBarController setSelectedIndex:_beforeIndex];
//            [self presentViewController:lvc animated:YES completion:^{
//                
//            }];
//        }else{
//            [tabBarController setSelectedIndex:_presentIndex];
//            _beforeIndex = _presentIndex;
//        }
//    }else{
//        [tabBarController setSelectedIndex:_presentIndex];
//        _beforeIndex = _presentIndex;
//    }
//    [self confirmSelectTabBar:_beforeIndex];
//    
//    [self setNoHighlightTabBar];
//    return YES;
//}
//- (void)setNoHighlightTabBar
//{
//    NSArray * tabBarSubviews = [self.tabBar subviews];
//    for(NSInteger i = [tabBarSubviews count]-1; i >= 0; i--)
//    {
//        for(UIView * v in [[tabBarSubviews objectAtIndex:i] subviews])
//        {
//            if(v && [NSStringFromClass([v class]) isEqualToString:@"UITabBarSelectionIndicatorView"])
//            {   //the v is the highlight view.
//                [v removeFromSuperview];
//                break;
//            }
//        }
//    }
//    
//}
//
//-(void)confirmSelectTabBar:(NSInteger )sender
//{
//    for (int i=10001; i<=10001+TABBARCOUNT; i++)
//    {
//        UIButton *temp=(UIButton*)[_bgView viewWithTag:i];
//        [temp setEnabled:temp.tag==sender+10001?NO:YES];
//    }
//    [self setNoHighlightTabBar];
//}
